﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Abp.AspNetCore.Mvc.Authorization;
using Abp.AspNetCore.Mvc.Extensions;
using FwProject.Authorization.Users;
using FwProject.Controllers;
using FwProject.Distribution;
using FwProject.Distribution.Dto;
using FwProject.Users;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;

namespace FwProject.Web.Mvc.Controllers
{
    [AbpMvcAuthorize]
    public class DistributionLineController : FwProjectControllerBase
    {
        private readonly IDistributionLineAppService _distributionLineAppService;
        private readonly IDriverLineAppService _driverLineAppService;
        private readonly IUserAppService _userAppService;

        public DistributionLineController(IDistributionLineAppService distributionLineAppService, IUserAppService userAppService,
            IDriverLineAppService driverLineAppService)
        {
            _distributionLineAppService = distributionLineAppService;
            _userAppService = userAppService;
            _driverLineAppService = driverLineAppService;
        }
        public async Task<IActionResult> Index(int pageIdx,int size,string LineName)
        {
            if (pageIdx == 0)
                pageIdx = 1;
            if (size == 0)
                size = 20;
            var model = await _distributionLineAppService.GetPagedListAsync(pageIdx, size, LineName);
            if (Request.IsAjaxRequest())
            {
                return PartialView("IndexPartial",model);
            }
            return View(model);
        }

        public IActionResult Create()
        {
            return View();
        }
        [HttpPost]
        public async Task<JsonResult> CreateLine(DistributionLineInput input)
        {
            await _distributionLineAppService.Create(input);
            return Json(1);
        }

        public async Task<IActionResult> Edit(Guid id)
        {
            var model = await _distributionLineAppService.GetByIdAsync(id);
            return View(model);
        }
        [HttpPost]
        public async Task<JsonResult> EditLine(Guid id,DistributionLineInput input)
        {
            await _distributionLineAppService.Update(id,input);
            return Json(id);
        }

        public async Task<JsonResult> Delete(Guid id)
        {
            await _distributionLineAppService.Delete(id);
            return Json(1);
        }
    }
}